package store.lunangangster.nodeflow.parser.el.operator;

import static store.lunangangster.nodeflow.enums.FlowType.FLOW_WHILE;

import cn.hutool.core.collection.CollUtil;
import com.ql.util.express.Operator;
import store.lunangangster.nodeflow.builder.FlowBuilder;
import store.lunangangster.nodeflow.core.component.WhileComponent;
import store.lunangangster.nodeflow.exception.RuleSourceParseException;
import store.lunangangster.nodeflow.parser.el.OperatorHelper;
import store.lunangangster.nodeflow.rt.element.Node;
import store.lunangangster.nodeflow.util.Converter;

/**
 * While表达式所关联的操作符
 *
 * @author cwk
 * @version 1.0
 */
public class WhileOperator extends Operator {

  @Override
  public Object executeInner(Object[] list) throws RuleSourceParseException {
    OperatorHelper.checkArguments(list);

    Node whileNode = Converter.convert(list[0], Node.class);
    if (!whileNode.isComponent(WhileComponent.class)) {
      throw new RuleSourceParseException("Incompatible executable for WhileFlow: should be like WHILE(whileNode).DO(flow) or WHILE(whileNode).DO(flow).EXIT(exitNode)");
    }
    return FlowBuilder.buildELFLow(FLOW_WHILE, CollUtil.toList(whileNode));
  }
}
